import 'package:flutter/material.dart';
import 'package:flutter_deer/l10n/deer_localizations.dart';
import 'package:flutter_deer/login/page/LoginPresenter.dart';
import 'package:flutter_deer/login/widgets/my_text_field.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/device_utils.dart';
import 'package:flutter_deer/widgets/load_image.dart';
import 'package:flutter_deer/widgets/my_button.dart';
import 'package:flutter_deer/widgets/my_scroll_view.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

/// design/1注册登录/index.html
class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  LoginPresenter get vm => Get.find<LoginPresenter>();

  void _login() {
    vm.login();
  }

  final _nodeText1 = FocusNode();
  final _nodeText2 = FocusNode();
  final _nodeText3 = FocusNode();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      /// 两种初始化方案，另一种见 main.dart
      /// 两种方法各有优劣
      await SpUtil.getInstance();
      await Device.initDeviceInfo();
      // if (SpUtil.getBool(Constant.keyGuide, defValue: true)!) {
      //   /// 预先缓存图片，避免直接使用时因为首次加载造成闪动
      //   void precacheImages(String image) {
      //     precacheImage(ImageUtils.getAssetImage(image, format: ImageFormat.webp), context);
      //   }
      //   _guideList.forEach(precacheImages);
      // }
      // _initSplash();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Row(children: <Widget>[
      Expanded(
        child: Center(
            child: Container(
          margin: const EdgeInsets.all(30),
          child: const LoadAssetImage('login/img_login'),
        )),
      ),
      Expanded(
        child: Center(
            child: Container(
                margin: const EdgeInsets.all(30),
                child: MyScrollView(
                  // keyboardConfig: Utils.getKeyboardActionsConfig(context, <FocusNode>[_nodeText1, _nodeText2, _nodeText3]),
                  padding: const EdgeInsets.only(left: 16.0, right: 16.0, top: 20.0),
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text('鹰策站控系统', style: TextStyles.textBold26, textAlign: TextAlign.center),
                    Gaps.vGap16,
                    MyTextField(
                      key: const Key('phone'),
                      focusNode: _nodeText1,
                      controller: vm.nameController,
                      maxLength: 11,
                      keyboardType: TextInputType.phone,
                      hintText: DeerLocalizations.of(context)!.inputUsernameHint,
                    ),
                    Gaps.vGap8,
                    MyTextField(
                      key: const Key('password'),
                      keyName: 'password',
                      focusNode: _nodeText2,
                      isInputPwd: true,
                      controller: vm.passwordController,
                      keyboardType: TextInputType.visiblePassword,
                      hintText: DeerLocalizations.of(context)!.inputPasswordHint,
                    ),
                    Gaps.vGap24,
                    Obx(() => MyButton(
                          key: const Key('login'),
                          onPressed: vm.canLogin.value ? _login : null,
                          // onPressed: _login,
                          text: DeerLocalizations.of(context)!.login,
                        )),
                    // 添加记住密码组件
                    Obx(() => Align(
                          alignment: Alignment.centerRight,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Checkbox(
                                value: vm.isChecked.value,
                                onChanged: (bool? value) {
                                  vm.onCheckedChanged(value);
                                },
                              ),
                              Text(
                                DeerLocalizations.of(context)!.forgotPasswordLink, // 假设存在记住密码的翻译
                                style: Theme.of(context).textTheme.titleSmall,
                              ),
                            ],
                          ),
                        )),
                    Gaps.vGap16,
                    Container(
                        alignment: Alignment.center,
                        child: GestureDetector(
                          child: Text(
                            DeerLocalizations.of(context)!.noAccountRegisterLink,
                            key: const Key('noAccountRegister'),
                            style: TextStyle(color: Theme.of(context).primaryColor),
                          ),
                          // onTap: () => NavigatorUtils.push(context, LoginRouter.registerPage),
                        )),
                    MyTextField(
                      key: const Key('snCode'),
                      focusNode: _nodeText3,
                      controller: vm.snCodeController,
                      maxLength: 20,
                      hintText: DeerLocalizations.of(context)!.inputUsernameHint,
                      // hintText: '输入绑定设备码',
                    ),
                  ],
                ))),
      ),
    ]));
  }
}
